<?php

namespace App\Models\User;

use App\Models\Common\CommonModel;
use App\Models\Constant\OrderConstant;
use App\Models\Order\Order;
use App\Models\Setting\SysSetting;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class User extends CommonModel
{
    use HasFactory;
    protected $table = 'user';
    protected $appends = ['is_finish_wx'];

    public function getIsFinishWxAttribute() {
        return !(empty($this->wx_nickname) || empty($this->wx_avatar) || empty($this->wx_samll_open_id) );
    }

    public function getRecords(array $conditions) {
        $records = parent::getRecords($conditions); // TODO: Change the autogenerated stub
        if(!empty($conditions['realName'])){
            $records = $records->where('real_name','like','%'.$conditions['realName'].'%');
        }

        return $records->orderBy('id','desc');
    }

    public function isAllowApply() {
        $applyNum = SysSetting::where('key','apply_num')->first()->val??6;
        return Order::where('user_id',$this->id)->whereNotIn('status',OrderConstant::MAP_NOT_EFFECT_APPLY)->count()<$applyNum;
    }

    public function isFirstApply($id=0):bool {
        return Order::where('user_id',$this->id)
                ->where('id','!=',$id)
                ->whereNotIn('status',OrderConstant::MAP_NOT_EFFECT_APPLY)->count()<=0;
    }

}
